package lee.vioson.ebook.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.outlined.Email
import androidx.compose.material.icons.outlined.Lock
import androidx.compose.material.icons.rounded.KeyboardArrowLeft
import androidx.compose.material.icons.rounded.KeyboardArrowRight
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import coil.compose.rememberAsyncImagePainter
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppDestinations
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.LoginViewModel

/**
 *Author:viosonlee
 *Date:2023/6/2
 *DESCRIPTION:
 */
@Composable
fun LoginScreen(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: LoginViewModel = viewModel(factory = viewModelFactory {
        initializer { LoginViewModel(appContainer) }
    })
) {
//    MviScreen(viewModel = viewModel) {
    val state by viewModel.state.collectAsState()
    val appConfig by viewModel.appConfig.collectAsState()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(horizontal = 16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .statusBarsPadding()
        ) {
            Box(
                modifier = Modifier
                    .size(48.dp)
                    .clip(CircleShape)
                    .background(AppTheme.colors.onBackground)
                    .clickable {
                        navHostController.popBackStack()
                    },
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    imageVector = Icons.Rounded.KeyboardArrowLeft,
                    contentDescription = "back",
                    modifier = Modifier.size(36.dp)
                )
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Image(
            painter = rememberAsyncImagePainter(model = appConfig?.loginLogo),
            contentDescription = "logo",
            contentScale = ContentScale.Crop,
            modifier = Modifier.size(120.dp)
        )
        Spacer(modifier = Modifier.height(50.dp))
        Text(
            text = stringResource(id = R.string.Login),
            style = TextStyle(fontSize = 28.sp, color = AppTheme.colors.onBackground)
        )
        Spacer(modifier = Modifier.height(24.dp))
        //email input
        InputFiled(
            modifier = Modifier.fillMaxWidth(),
            value = state.email,
            onValueChange = { viewModel.sendEvent(LoginViewModel.Event.OnEmailChange(email = it)) },
            placeHolder = R.string.Email,
            error = state.emailError,
            leftIcon = {
                Icon(
                    imageVector = Icons.Outlined.Email,
                    contentDescription = "input email"
                )
            },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
        )
        Spacer(modifier = Modifier.height(24.dp))
        // passwd input
        InputFiled(
            modifier = Modifier.fillMaxWidth(),
            value = state.password,
            onValueChange = { viewModel.sendEvent(LoginViewModel.Event.OnPwdChange(password = it)) },
            placeHolder = R.string.Password,
            error = state.passwordError,
            leftIcon = {
                Icon(imageVector = Icons.Outlined.Lock, contentDescription = "input password")
            },
            isPassword = true,
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
        )
        Spacer(modifier = Modifier.height(60.dp))
        //login button
        Row(Modifier.fillMaxWidth()) {
            Button(
                modifier = Modifier.height(54.dp),
                shape = RoundedCornerShape(size = 16.dp),
                colors = AppTheme.loginButtonColors(),
                enabled = state.enable,
                onClick = { viewModel.login(state.email, state.password) }
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(text = stringResource(id = R.string.Login), fontSize = 18.sp)
                    Icon(
                        imageVector = Icons.Rounded.KeyboardArrowRight,
                        contentDescription = "login button arrow"
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(24.dp))
        //create new
        Row(Modifier.fillMaxWidth()) {
            Text(
                text = stringResource(id = R.string.You_are_new),
                fontSize = 14.sp,
                color = AppTheme.appColors.loginTipsTextColor
            )
            Spacer(modifier = Modifier.width(6.dp))
            Text(
                text = stringResource(id = R.string.Create_new),
                fontSize = 14.sp,
                color = AppTheme.appColors.createNewTextColor,
                modifier = Modifier.clickable {
                    navHostController.navigate(AppDestinations.REGISTER_ROUTE)
                }
            )
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = stringResource(id = R.string.Forget_Password),
                fontSize = 14.sp,
                color = AppTheme.appColors.loginTipsTextColor,
                modifier = Modifier.clickable {
                    navHostController.navigate(AppDestinations.SET_PASSWORD_ROUTE)
                }
            )
        }
        Spacer(modifier = Modifier.height(24.dp))
        //oss login
        Row {
            Icon(
                imageVector = Icons.Default.Settings,
                contentDescription = "google login",
                modifier = Modifier.size(48.dp),
                tint = AppTheme.appColors.ossLoginIconTint
            )
            Spacer(modifier = Modifier.width(20.dp))
            Icon(
                imageVector = Icons.Default.Settings,
                contentDescription = "google login",
                modifier = Modifier.size(48.dp),
                tint = AppTheme.appColors.ossLoginIconTint
            )
            Spacer(modifier = Modifier.width(20.dp))
            Icon(
                imageVector = Icons.Default.Settings,
                contentDescription = "google login",
                modifier = Modifier.size(48.dp),
                tint = AppTheme.appColors.ossLoginIconTint
            )
        }
    }
}

//}